<?php
namespace Realisation\Web\Builder;

/**
 * Description of JsonBuilder
 *
 * @author pahhan
 */
class JsonBuilder implements BuilderInterface
{
    /**
     * Returns error element
     * @param integer $code
     * @param string $message
     * @return array
     */
    private function createErrorElement($code = 0, $message = NULL)
    {
        $element = array(
            'code' => $code,
            'message' => $message,
        );

        return $element;
    }

    public function buildMessages(\Doctrine\DBAL\Driver\Statement $stmt)
    {
        $out = array(
            'error' => $this->createErrorElement(),
            'messages' => $stmt->fetchAll()
        );

        return json_encode($out);
    }

    public function buildMessagesCount(\Doctrine\DBAL\Driver\Statement $stmt)
    {
        $out = array(
            'error' => $this->createErrorElement(),
            'count' => $stmt->fetchColumn()
        );

        return json_encode($out);
    }

    public function buildSites(\Doctrine\DBAL\Driver\Statement $stmt)
    {
        $out = array(
            'error' => $this->createErrorElement(),
            'sites' => $stmt->fetchAll()
        );

        return json_encode($out);
    }

    public function buildUrl(\Doctrine\DBAL\Driver\Statement $stmt)
    {
        $out = array(
            'error' => $this->createErrorElement(),
            'urls' => $stmt->fetchAll()
        );

        return json_encode($out);
    }
}

